// pages/circle_detail/index.js
var common = require('../../lib/common.js');
var url = require('../../config/url.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: 0,
        // 轮播图
        slideshowData: [],
        // 浮动按钮
        floatingBtn: [{
            type: "homepage",
            title: "首页",
            span: 8
        }, {
            type: "collection",
            title: "收藏",
            span: 8
        }, {
            type: "share",
            title: "分享",
            span: 8,
            openType: "share"
        }],
        circle: {},
        photosData: [],
        articles: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ id: options.id });
        this.getDetail();
        this.getNewList();
        this.addLookNum();
        this.getCollect();
    },

    getDetail() {
        common.post(url.getCircle, { id: this.data.id }, (data) => {
            let slideshow = data.slideshow.split(',');

            data.slideshow = slideshow.map(n => {
                return { image: n }
            });

            for (let i in data.type) {
                data.type[i].two_type_name = data.type[i].two_type_name.replace(/,/g, '、');
            }

            this.setData({
                circle: data
            })

            wx.setNavigationBarTitle({
                title: data.name
            })

            this.getArticleListById();
        });
    },

    // 选择浮动按钮
    onFloatingClicked(e) {
        let index = e.detail.index;
        switch (index) {
            case 0:
                wx.switchTab({
                    url: '/pages/index/index'
                })
                break;
            case 1:
                this.onTaggleCollect();
                break;
            case 2:
                wx.showShareMenu({
                    withShareTicket: true
                });
                break;
        }
    },

    callMobile({ currentTarget }) {
        wx.makePhoneCall({
            phoneNumber: currentTarget.dataset.mobile + ''
        })
    },

    showLocation({ currentTarget }) {
        var data = currentTarget.dataset;
        wx.openLocation({
            latitude: data.latitude,
            longitude: data.longitude,
            scale: 18
        })
    },

    /**
     * 私聊
     * @param {*} param0 
     */
    onChat({ currentTarget }) {
        var data = currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/chat/index?userid=' + data.userid + '&username=' + data.username
        })
    },

    /**
     * 获取最新照片列表
     */
    getNewList() {
        common.post(url.getCirclePhotoNewList, { circle_id: this.data.id }, (data) => {
            for (let i in data) {
                data[i].url = '/pages/circle_photos_detail/index?id=' + data[i].id;
            }
            this.setData({ photosData: data });
        })
    },

    getArticleListById() {
        common.post(url.getArticleListById, { user_id: this.data.circle.user_id }, (data) => {
            for (let i in data) {
                data[i].url = '/pages/aritcle_detail/index?id=' + data[i].id
                if (data[i].images == null)
                    data[i].images = [];
                else {
                    data[i].images = (data[i].images + '').split(',');
                    data[i].images = data[i].images.map((n) => {
                        return n;
                    })
                }
            }
            this.setData({ articles: data });
        })
    },

    /**
     * 添加阅读量
     */
    addLookNum() {
        common.post(url.addCircleLookNum, { circle_id: this.data.id }, (data) => {
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        wx.showShareMenu({
            withShareTicket: true
        });

        this.addArticleShareNum();
    },

    getCollect() {
        common.post(url.getCollectCircle, { circle_id: this.data.id }, (data) => {
            this.taggleCollect(data);
        })
    },

    taggleCollect(state) {
        let floatingBtn = this.data.floatingBtn;
        if (state) {
            floatingBtn[1].type = 'collection_fill';
            floatingBtn[1].title = '取消收藏';
        } else {
            floatingBtn[1].type = 'collection';
            floatingBtn[1].title = '收藏';
        }
        this.setData({
            floatingBtn
        });
    },

    onTaggleCollect() {
        wx.showLoading({ mask: true });
        common.post(url.toggleCollectCircle, { circle_id: this.data.id }, (data) => {
            wx.hideLoading();
            this.taggleCollect(data);
        })
    },

    onMorePhoto() {
        wx.navigateTo({
            url: '/pages/circle_photos/index?id=' + this.data.id
        })
    },

    onAddPhoto() {
        wx.navigateTo({
            url: '/pages/my_circle/index'
        })
    },

    onTabChange() {

    },

    /**
    * 添加文章分享数
    */
    addArticleShareNum() {
        common.post(url.addCircleShareNum, { circle_id: this.data.id }, () => { });
    },

})